/*
 * jQuery Switcher plugin
 * Version 1.2 (23/10/2012)
 * Licensed under the GPL license
 * @requires jQuery v1.7.0 or later
 *
 * Copyright (c) 2012 Brisa Francesco Angelo
 * fbrisa@gmail.com
 
 USAGE:
 
 $(".someclass").switcher();
 $("#switcherDivId").switcher();
 
 WITH OPTIONS:
 $("#switcherDivId").switcher({
 textTrue: "OK",                 // Text for True label
 trueColor: "yellow",            // Color for true label
 trueBackground: "#004000",      // Background color for true label
 
 textFalse: "NO",                // Text for False label
 falseColor: "black",            // Color for false label
 falseBackground: "#AA0000",     // Background color for false label
 
 duration: 200,          // Animation time
 
 // Option function called before the change
 // If it returns true, change occures
 // If it returns false, change does not happen
 callbackBefore: function(newValue) { 
 return true;
 },
 
 // Option function called after the change is done
 callbackAfter: function(newValue) {} 
 });
 */
(function($) {
    // *****************
    // Private functions
    // *****************
    function _refresh() {
        var $this = $(this);
        var data = $this.data('switcher');

        if (data.value) {
            $this.find(".txtSwOff").animate({marginLeft: "-60px"}, data.duration, function() {
            });
        } else {
            $this.find(".txtSwOff").animate({marginLeft: 0}, data.duration, function() {
            });
        }
    }

    function _changeValue(newValue) {
        var $this = $(this), data = $this.data('switcher');
        if (data.value !== newValue) {

            var youCanChange = true;
            var oldValue = data.value;

            if (data.callbackBefore !== null) {
                if (typeof data.callbackBefore === 'function') { // make sure the callback is a function
                    if (data.value !== newValue) {
                        youCanChange = data.callbackBefore.call(this, oldValue, newValue); // brings the scope to the callback
                    }
                }
            }

            if (youCanChange) {

                data.value = newValue;
                _refresh.call(this);

                if (data.callbackAfter !== null) {
                    if (typeof data.callbackAfter === 'function') { // make sure the callback is a function                    
                        data.callbackAfter.call(this, oldValue, data.value); // brings the scope to the callback                                            
                    }
                }

            }

        }
    }

    // ****************
    // Public functions
    // ****************
    var methods = {
        init: function(options) {
            return this.each(function() {
                var $this = $(this);
                var data = $this.data('switcher');

                // If the plugin hasn't been initialized yet
                if (!data) {

                    var nd = $.extend({
                        textTrue: "ON",
                        trueColor: null,
                        trueBackground: null,
                        textFalse: "OFF",
                        falseColor: null,
                        falseBackground: null,
                        duration: 100,
                        value: false,
                        callbackBefore: null,
                        callbackAfter: null,
                        color: "#FFF",
                        disabled: false
                    }, options);

                    $(this).data('switcher', nd);

                    data = $this.data('switcher');
                }

                $this.html("");
                $this.addClass("bool-slider").addClass("false");
                $this.html("<div class='inset'><div style='width : 200px;overflow:hidden;height: 100%;'><div class='txtSwOff'></div><div class='swbutton'><span style='margin:0 auto;' class='ui-icon ui-icon-grip-dotted-vertical'></span></div><div class='txtSwOn'></div></div></div>");
                $this.find(".txtSwOn").html(data.textTrue);
                $this.find(".txtSwOff").html(data.textFalse);

                if (data.trueColor !== null) {
                    $this.find(".txtSwOn").css("color", data.trueColor);
                }
                if (data.trueBackground !== null) {
                    $this.find(".txtSwOn").css("background-color", data.trueBackground);
                }

                if (data.falseColor !== null) {
                    $this.find(".txtSwOff").css("color", data.falseColor);
                }
                if (data.falseBackground !== null) {
                    $this.find(".txtSwOff").css("background-color", data.falseBackground);
                }


                if($.ui){
                    // jQuery UI is loaded
                    $this.addClass("ui-widget").addClass("ui-widget-content").addClass("ui-corner-all");
                    
                    $this.find(".txtSwOn").addClass("ui-state-active");
                    $this.find(".txtSwOff").addClass("ui-state-default");
                    $this.find(".swbutton").addClass("ui-state-default");
                    
                    $(".swbutton").hover(
                        function() {  
                            $(this).addClass('ui-state-hover');  
                        },  
                        function() {  
                            $(this).removeClass('ui-state-hover');
                        }    
                    );
                } else {
                    $this.css("border","1px solid #CCC");
                    $this.css("color",data.color);
                    $this.css("border-radius","5px");
                    $this.css("font-weight","800");
                    //$this.find(".inset .txtSwOn").css("border-radius","0px 5px 5px 0px");                    
                    //$this.find(".inset .txtSwOff").css("border-radius","5px 0px 0px 5px");                    
                    $this.find(".swbutton").css("background-color","#f5f5f5");
                    $this.find(".txtSwOn").css("background-color", "#00A000");
                    $this.find(".txtSwOff").css("background-color", "#DA4F49");
                }


                if (! ($.browser.msie  && parseInt($.browser.version, 10) <= 8)) {
                    if (jQuery().swipe) {
                        // if swipe plugin is loaded, activate swipe gesture
                        $this.swipe({
                            swipeLeft: function() {
                                _changeValue.call($this, true);
                            },
                            swipeRight: function() {
                                _changeValue.call($this, false);
                            },
                            threshold: {x: 0, y: 100}
                        });
                    }
                }



                $this.unbind("click").click(function() {
                    if (!data.disabled) {
                        _changeValue.call(this, !data.value);
                    }
                });

                _refresh.call(this);

            });
        },
        destroy: function( ) {
            return this.each(function() {
                var $this = $(this), data = $this.data('switcher');

                // Namespacing FTW
                //$(window).unbind('.tooltip');
                //data.tooltip.remove();
                //$this.removeData('tooltip');

            });
        },
        val: function(newValue) {
            if (newValue !== undefined) {
                if (newValue === true || newValue === false || newValue === 0 || newValue === 1) {
                    if (newValue === 0 || newValue === "0") {
                        newValue = false;
                    }
                    if (newValue === 1 || newValue === "1") {
                        newValue = true;
                    }
                    return this.each(function() {
                        var $this = $(this), data = $this.data('switcher');
                        _changeValue.call(this, newValue);
                    });
                }
            }

            var $this = $(this), data = $this.data('switcher');
            return data.value;
        },
        valNumeric: function() {
            var $this = $(this), data = $this.data('switcher');
            if (data.value)
                return 1;
            return 0;
        },
        text: function() {
            var $this = $(this), data = $this.data('switcher');
            if (data.value) {
                return data.textTrue;
            }
            return data.textFalse;
        },
        disable: function() {
            return this.each(function() {
                var $this = $(this), data = $this.data('switcher');
                data.disabled=true;
                if($.ui){
                    $this.addClass("ui-state-disabled");
                } else {
                    $this.addClass("disabled");                    
                }

            });
        },
        enable: function() {
            return this.each(function() {
                var $this = $(this), data = $this.data('switcher');
                data.disabled=false;
                if($.ui){
                    $this.removeClass("ui-state-disabled");
                } else {
                    $this.removeClass("disabled");
                }
                

            });
        }

    };

    $.fn.switcher = function(method) {

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.switcher');
        }

    };

})(jQuery);